package com.hfi.ood.payroll.domain;

import lombok.Builder;
import lombok.Data;
import org.joda.money.Money;

import java.util.Date;

/**
 * @author ChangLiang
 * @date 2020/5/19
 */
@Data
@Builder
public class Employee {

    private Long id;

    private String name;

    private String address;

    private PaymentClassification paymentClassification;

    private PaymentMethod paymentMethod;

    private PaymentSchedule paymentSchedule;

    private Affiliation affiliation;

    public boolean isPayDay(Date date) {
        return this.paymentSchedule.isPayDay(date);
    }

    public Date getPayPeriodStartDate(Date date) {
        return this.paymentSchedule.getPayPeriodStartDate(date);
    }

    /**
     * 根据工资条 发放工资
     * @param paycheck
     */
    public void payDay(Paycheck paycheck) {
        Money grossPay = this.paymentClassification.calculatePay(paycheck);
        Money deduction = affiliation.calculateDeduction(paycheck);
        Money netPay = grossPay.minus(deduction);
        paycheck.setGrossPay(grossPay);
        paycheck.setDeductions(deduction);
        paycheck.setNetPay(netPay);
        this.paymentMethod.pay(paycheck);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
